package com.lankystar.sos.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.sos.common.Page;
import com.lankystar.sos.dao.IMessageDao;
import com.lankystar.sos.model.Message;
import com.lankystar.util.DateUtil;

@Repository
public class MessageDaoImpl implements IMessageDao{

	Page<Message> page = new Page<>();
	@Autowired
	private SessionFactory sf;
	public Session getSession(){
		return sf.getCurrentSession();
	}
	@Override
	public void addMessage(Message record) {
		getSession().save(record);
	}
	@SuppressWarnings("unchecked")
	@Override
	public Page<Message> selectMessageBysos_id(Integer sos_id,int pageSize,int pageNow) {
		
		String sql = "from Message m join m.user u join m.room r where r.sos_id="+sos_id+"";
		String count_sql = "select count(*) from Message m join m.user u join m.room r where r.sos_id="+sos_id+"";
		List<Object[]> oblist = getSession().createQuery(sql).list();
		long count = (long) getSession().createQuery(count_sql)
										.setMaxResults(pageSize)
										.setFirstResult((pageNow-1)*pageSize)
										.uniqueResult();
		page.setDatas(oblist);
		page.setPageSize(pageSize);
		page.setPageNow(pageNow);
		int rowCount = (int) count;
		page.setRowCount(rowCount);
		int pageCount = 0;
		if(rowCount % pageSize == 0){
			pageCount = rowCount / pageSize;
		}else {
			pageCount = rowCount / pageSize +1;
		}
		page.setPageCount(pageCount);
		return page;
	}
	/**
	 *向上查询type=1、type为其他值时--向下查询最新消息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Page<Message> selectMessageByTime(Integer sos_id, int pageSize, int pageNow,String type) {
		String sql="";
		String count_sql="";
		if(type=="1"){
			sql = "from Message m join m.user u join m.room r where r.sos_id="+sos_id+" and m.time < "+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+"";
			count_sql = "select count(*) from Message m join m.user u join m.room r where r.sos_id="+sos_id+" and m.time > "+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+"";
		}else{
			sql = "from Message m join m.user u join m.room r where r.sos_id="+sos_id+" and m.time > "+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+"";
			count_sql = "select count(*) from Message m join m.user u join m.room r where r.sos_id="+sos_id+" and m.time > "+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+"";
		}
		List<Object[]> oblist = getSession().createQuery(sql).list();
		long count = (long) getSession().createQuery(count_sql)
										.setMaxResults(pageSize)
										.setFirstResult((pageNow-1)*pageSize)
										.uniqueResult();
		page.setDatas(oblist);
		page.setPageSize(pageSize);
		page.setPageNow(pageNow);
		int rowCount = (int) count;
		page.setRowCount(rowCount);
		int pageCount = 0;
		if(rowCount % pageSize == 0){
			pageCount = rowCount / pageSize;
		}else {
			pageCount = rowCount / pageSize +1;
		}
		page.setPageCount(pageCount);
		return page;
	}

}
